home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / twocolumn ldef / twocolldef.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.6 KB  |  151 lines

  1. /*
  2.     File:        TwoColLDEF.c
  3.  
  4.     Contains:    Two-column LDEF code resource
  5.     
  6.                 looks for a comma in the text of each cell and draws
  7.                 the text that follows the comma half-way across the cell
  8.     
  9.                 so a cell containing the text
  10.                 +--------------+
  11.                 | abc,def      |
  12.                 +--------------+
  13.                 will instead appear as
  14.                 +--------------+
  15.                 | abc   def    |
  16.                  +--------------+
  17.         
  18.                 To see this LDEF in action, paste it into the ModalList
  19.                 sample program and recompile the program so that the LNew call uses
  20.                 the definition procedure 128 rather than 0
  21.  
  22.     Written by: Greg Robbins    
  23.  
  24.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  25.  
  26.                 You may incorporate this Apple sample source code into your program(s) without
  27.                 restriction. This Apple sample source code has been provided "AS IS" and the
  28.                 responsibility for its operation is yours. You are not permitted to redistribute
  29.                 this Apple sample source code as "Apple sample source code" after having made
  30.                 changes. If you're going to re-distribute the source, we require that you make
  31.                 it clear in the source that the code was descended from Apple sample source
  32.                 code, but that you've made changes.
  33.  
  34.     Change History (most recent first):
  35.                 8/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  36.                 
  37.  
  38. */
  39. #include <Lists.h>
  40. #include <ToolUtils.h>
  41. #include <String.h>
  42. #include <LowMem.h>
  43.  
  44. /* constants for spacing */
  45.  
  46. #define kLeftOffset    2
  47. #define kTopOffset    0
  48.  
  49.  
  50. /* main LDEF entry point */
  51.  
  52. pascal void    main(short lMessage,Boolean lSelect,Rect *lRect,Cell lCell,
  53.                 short lDataOffset,short lDataLen,ListHandle lHandle)
  54. {
  55.     FontInfo fontInfo;                        /* font information (ascent/descent/etc)  */
  56.     ListPtr listPtr;                        /* pointer to store dereferenced list     */
  57.     SignedByte hStateList,hStateCells;        /* state variables for HGetState/SetState */
  58.     Ptr cellData;                            /* points to start of cell data for list  */
  59.     short leftDraw,topDraw;                    /* left/top offsets from topleft of cell  */
  60.     short halfwayLeftDraw;                    /* half way across from left of cell      */
  61.     short substringLen;                        /* number of characters before comma      */
  62.     Ptr commaPtr;                            /* substring beginning at comma           */
  63.     Point currPenPt;                        /* current pen location                   */
  64.     Rect fullCellRect;                      /* unclipped size of cell                 */
  65.     
  66.     /* lock and dereference list mgr handles */
  67.     
  68.     hStateList = HGetState((Handle) lHandle);
  69.     HLock((Handle) lHandle);
  70.     listPtr = *lHandle;
  71.     
  72.     hStateCells = HGetState(listPtr->cells);
  73.     HLock(listPtr->cells);
  74.     
  75.     cellData = *(listPtr->cells);
  76.     
  77.     /* get the size of the cells (since lRect may be clipped) */
  78.     
  79.     LRect(&fullCellRect, lCell, lHandle);
  80.     
  81.     switch (lMessage) {
  82.       case lInitMsg:
  83.           /* we don't need any initialization */
  84.           break;
  85.  
  86.       case lDrawMsg:
  87.         EraseRect(lRect);
  88.         
  89.           if (lDataLen > 0) {
  90.           
  91.               /* determine starting point for drawing */
  92.               
  93.               leftDraw = lRect->left + listPtr->indent.h + kLeftOffset;
  94.               topDraw = lRect->top + listPtr->indent.v + kTopOffset;
  95.               
  96.             /* move to the text location */
  97.             GetFontInfo(&fontInfo);
  98.             MoveTo(leftDraw, topDraw + fontInfo.ascent);
  99.             
  100.             /* if there's a comma, only draw the characters up to the comma
  101.                and then move the data offset and length variables to
  102.                point to the rest of the string following that comma */
  103.              
  104.             /* look for a comma, using memchr from the ANSI libraries (yech) */
  105.             commaPtr = memchr(cellData + lDataOffset, ',', lDataLen);
  106.             
  107.             if (commaPtr != nil) {
  108.             
  109.                 /* draw the substring preceding the comma */
  110.                 substringLen = (short) (commaPtr - (cellData + lDataOffset));
  111.                 DrawText(cellData, lDataOffset, substringLen);
  112.                 
  113.                 /* move the offset to skip the first substring and comma */
  114.                 lDataOffset += substringLen + 1;
  115.                 lDataLen -= (substringLen + 1);
  116.                 
  117.                 /* move to half way across the cell unless the pen
  118.                    is already to the right of that point */
  119.                    
  120.                 halfwayLeftDraw = (fullCellRect.right + leftDraw) / 2;
  121.                 GetPen(&currPenPt);
  122.                 if (currPenPt.h < halfwayLeftDraw)
  123.                     MoveTo(halfwayLeftDraw, topDraw+fontInfo.ascent);
  124.             }
  125.             
  126.             /* draw all remaining characters in the cell */
  127.             DrawText(cellData, lDataOffset, lDataLen);
  128.  
  129.           }
  130.  
  131.         if (!lSelect)
  132.               break;
  133.         
  134.       case lHiliteMsg:
  135.           /* clearing the HiliteMode bit forces the next Invert to really
  136.              be a "hilite" mode inversion */
  137.  
  138.           BitClr((Ptr)LMGetHiliteMode,pHiliteBit);
  139.           InvertRect(lRect);
  140.           break;
  141.  
  142.       case lCloseMsg:
  143.           break;
  144.     }
  145.     
  146.     /* restore the handles to their original states */
  147.     HSetState(listPtr->cells,hStateCells);
  148.     HSetState((Handle) lHandle,hStateList);
  149. }
  150.  
  151.